<?php
class BonusSummaryPage extends MemberPage {
    private static $default_parent = 'CommissionPage';

    private static $db = array();

    private static $has_one = array();

}

class BonusSummaryPage_Controller extends MemberPage_Controller {

    /**
     * An array of actions that can be accessed via a request. Each array element
     * should be an action name, and the
     * permissions or conditions required to allow the user to access it.
     *
     * <code>
     * array (
     *     'action', // anyone can access this action
     *     'action' => true, // same as above
     *     'action' => 'ADMIN', // you must have ADMIN permissions to access this
     * action
     *     'action' => '->checkAction' // you can only access this action if
     * $this->checkAction() returns true
     * );
     * </code>
     *
     * @var array
     */
    private static $allowed_actions = array(
        'Form',
        'BS1',
        'BS1Form',
        'BS2',
        'BS2Form',
        'BS3',
        'BS3Form',
        'BS4',
        'BS4Form'
    );

    public function init() {
        parent::init();
    }
	
	function TodayBonus(){
		$content = '<div class="table-responsive"><table class="table table-bordered table-striped table-hover">';
		$content .= sprintf('<thead><tr><th>%s</th><th class="text-right">%s</th><th class="text-right">%s</th><th class="text-right">%s</th><th class="text-right">%s</th></tr></thead>', _t('BonusSummaryPage.TODAY_BONUS', 'Today Bonus'), _t('BonusSummaryPage.UNILEVEL_BONUS', 'Unilevel Bonus'), _t('BonusSummaryPage.INVEST_BONUS', 'Invest Bonus'), _t('BonusSummaryPage.MATCHING_BONUS', 'Matching Bonus'), _t('BonusSummaryPage.PAIRING_BONUS', 'Pairing Bonus'));
		
		$batch_id = BonusBatch::get_current_batch_id();
		$bs1 = UnilevelBonus::get()->filter('MemberID', $this->CurrentMember()->ID)->filter('BatchID', $batch_id)->sum('Credit') - UnilevelBonus::get()->filter('MemberID', $this->CurrentMember()->ID)->filter('BatchID', $batch_id)->sum('Debit');
		$bs2 = InvestProgramPayout::get()->filter('InvestProgram.MemberID', $this->CurrentMember()->ID)->filter('BatchID', $batch_id)->sum('Credit') - InvestProgramPayout::get()->filter('InvestProgram.MemberID', $this->CurrentMember()->ID)->filter('BatchID', $batch_id)->sum('Debit');
		$bs3 = MatchingBonus::get()->filter('MemberID', $this->CurrentMember()->ID)->filter('BatchID', $batch_id)->sum('Credit') - MatchingBonus::get()->filter('MemberID', $this->CurrentMember()->ID)->filter('BatchID', $batch_id)->sum('Debit');
		$bs4 = PairingBonus::get()->filter('MemberID', $this->CurrentMember()->ID)->filter('BatchID', $batch_id)->sum('Credit') - PairingBonus::get()->filter('MemberID', $this->CurrentMember()->ID)->filter('BatchID', $batch_id)->sum('Debit');
		
		$bs1_link = sprintf('<a href="%s" title="%s" data-title="%s" rel="popup tooltip" data-modal-lg="true">%s</a>', sprintf('%s?%s=%s', $this->Link('BS1'), 'BatchID', $batch_id), _t('BonusSummaryPage.CLICK_PAYOUT_DETAILS', 'Click here to view payout details'), _t('BonusSummaryPage.PAYOUT_DETAILS', 'Payout Details'), DBField::create_field('Currency', $bs1)->Nice());
		$bs2_link = sprintf('<a href="%s" title="%s" data-title="%s" rel="popup tooltip" data-modal-lg="true">%s</a>', sprintf('%s?%s=%s', $this->Link('BS2'), 'BatchID', $batch_id), _t('BonusSummaryPage.CLICK_PAYOUT_DETAILS', 'Click here to view payout details'), _t('BonusSummaryPage.PAYOUT_DETAILS', 'Payout Details'), DBField::create_field('Currency', $bs2)->Nice());
		$bs3_link = sprintf('<a href="%s" title="%s" data-title="%s" rel="popup tooltip" data-modal-lg="true">%s</a>', sprintf('%s?%s=%s', $this->Link('BS3'), 'BatchID', $batch_id), _t('BonusSummaryPage.CLICK_PAYOUT_DETAILS', 'Click here to view payout details'), _t('BonusSummaryPage.PAYOUT_DETAILS', 'Payout Details'), DBField::create_field('Currency', $bs3)->Nice());
		$bs4_link = sprintf('<a href="%s" title="%s" data-title="%s" rel="popup tooltip" data-modal-lg="true">%s</a>', sprintf('%s?%s=%s', $this->Link('BS4'), 'BatchID', $batch_id), _t('BonusSummaryPage.CLICK_PAYOUT_DETAILS', 'Click here to view payout details'), _t('BonusSummaryPage.PAYOUT_DETAILS', 'Payout Details'), DBField::create_field('Currency', $bs4)->Nice());
		
		$content .= sprintf('<tbody><tr><td></td><td class="text-right">%s</td><td class="text-right">%s</td><td class="text-right">%s</td><td class="text-right">%s</td></tr></tbody>', $bs1_link, $bs2_link, $bs3_link, $bs4_link);
		$content .= '</table></div>';
		
		return $content;
	}

    function Form() {
        $fields = FieldList::create(
            DateRangeField::create('Batch__Date', _t('BonusSummaryPage.DATE', 'Date'))
        );

        $field_list = array(
            'Batch.Date' => _t('BonusSummaryPage.DATE', 'Date'),
            'BS1Link' => array('title' => _t('BonusSummaryPage.UNILEVEL_BONUS', 'Unilevel Bonus'), 'classes' => 'text-right'),
            'BS2Link' => array('title' => _t('BonusSummaryPage.INVEST_BONUS', 'Invest Bonus'), 'classes' => 'text-right'),
            'BS3Link' => array('title' => _t('BonusSummaryPage.MATCHING_BONUS', 'Matching Bonus'), 'classes' => 'text-right'),
            'BS4Link' => array('title' => _t('BonusSummaryPage.PAIRING_BONUS', 'Pairing Bonus'), 'classes' => 'text-right'),
        );
        $casting_list = array(
            'Batch.Date' => 'Date->Nice'
        );

        return DataListSearchForm::create($this, 'Form', 'BonusSummary', array('MemberID' => $this->CurrentMember()->ID), $fields)->setDataFieldList($field_list)->setFieldCasting($casting_list);
    }

	function BS1(){
		return $this->BS1Form()->forTemplate();
	}

	function BS1Form(){
		$fields = FieldList::create();
		$actions = FieldList::create();

        $field_list = array(
            'Description' => _t('BonusSummaryPage.BONUS_DESCRIPTION', 'Description'),
            'AmountText' => array('title' => _t('BonusSummaryPage.AMOUNT', 'Amount ({currency})', '', array('currency' => SiteCurrencyConfig::current_site_currency())), 'classes' => 'text-right'),
            'ForwardBalance' => array('title' => _t('BonusSummaryPage.BALANCE', 'Balance ({currency})', '', array('currency' => SiteCurrencyConfig::current_site_currency())), 'classes' => 'text-right')
        );
        $casting_list = array(
            'ForwardBalance' => 'Double->Nice'
        );

        return DataListSearchForm::create($this, 'BS1Form', 'UnilevelBonus', array('MemberID' => $this->CurrentMember()->ID, 'BatchID' => $this->request->requestVar('BatchID')), $fields, $actions)->setDataFieldList($field_list)->setFieldCasting($casting_list)->setFormAction(sprintf('%s?%s=%s', $this->Link('BS1Form'), 'BatchID', $this->request->requestVar('BatchID')));
	}

	function BS2(){
		return $this->BS2Form()->forTemplate();
	}

	function BS2Form(){
		$fields = FieldList::create();
		$actions = FieldList::create();

        $field_list = array(
            'Description' => _t('BonusSummaryPage.BONUS_DESCRIPTION', 'Description'),
            'AmountText' => array('title' => _t('BonusSummaryPage.AMOUNT', 'Amount ({currency})', '', array('currency' => SiteCurrencyConfig::current_site_currency())), 'classes' => 'text-right'),
            'ForwardBalance' => array('title' => _t('BonusSummaryPage.BALANCE', 'Balance ({currency})', '', array('currency' => SiteCurrencyConfig::current_site_currency())), 'classes' => 'text-right')
        );
        $casting_list = array(
            'ForwardBalance' => 'Double->Nice'
        );

        return DataListSearchForm::create($this, 'BS2Form', 'InvestProgramPayout', array('InvestProgram.MemberID' => $this->CurrentMember()->ID, 'BatchID' => $this->request->requestVar('BatchID')), $fields, $actions)->setDataFieldList($field_list)->setFieldCasting($casting_list)->setFormAction(sprintf('%s?%s=%s', $this->Link('BS2Form'), 'BatchID', $this->request->requestVar('BatchID')));
	}

	function BS3(){
		return $this->BS3Form()->forTemplate();
	}

	function BS3Form(){
		$fields = FieldList::create();
		$actions = FieldList::create();

        $field_list = array(
            'Description' => _t('BonusSummaryPage.BONUS_DESCRIPTION', 'Description'),
            'AmountText' => array('title' => _t('BonusSummaryPage.AMOUNT', 'Amount ({currency})', '', array('currency' => SiteCurrencyConfig::current_site_currency())), 'classes' => 'text-right'),
            'ForwardBalance' => array('title' => _t('BonusSummaryPage.BALANCE', 'Balance ({currency})', '', array('currency' => SiteCurrencyConfig::current_site_currency())), 'classes' => 'text-right')
        );
        $casting_list = array(
            'ForwardBalance' => 'Double->Nice'
        );

        return DataListSearchForm::create($this, 'BS3Form', 'MatchingBonus', array('MemberID' => $this->CurrentMember()->ID, 'BatchID' => $this->request->requestVar('BatchID')), $fields, $actions)->setDataFieldList($field_list)->setFieldCasting($casting_list)->setFormAction(sprintf('%s?%s=%s', $this->Link('BS3Form'), 'BatchID', $this->request->requestVar('BatchID')));
	}

	function BS4(){
		return $this->BS4Form()->forTemplate();
	}

	function BS4Form(){
		$fields = FieldList::create();
		$actions = FieldList::create();

        $field_list = array(
            'Description' => _t('BonusSummaryPage.BONUS_DESCRIPTION', 'Description'),
            'AmountText' => array('title' => _t('BonusSummaryPage.AMOUNT', 'Amount ({currency})', '', array('currency' => SiteCurrencyConfig::current_site_currency())), 'classes' => 'text-right'),
            'ForwardBalance' => array('title' => _t('BonusSummaryPage.BALANCE', 'Balance ({currency})', '', array('currency' => SiteCurrencyConfig::current_site_currency())), 'classes' => 'text-right')
        );
        $casting_list = array(
            'ForwardBalance' => 'Double->Nice'
        );

        return DataListSearchForm::create($this, 'BS4Form', 'PairingBonus', array('MemberID' => $this->CurrentMember()->ID, 'BatchID' => $this->request->requestVar('BatchID')), $fields, $actions)->setDataFieldList($field_list)->setFieldCasting($casting_list)->setFormAction(sprintf('%s?%s=%s', $this->Link('BS4Form'), 'BatchID', $this->request->requestVar('BatchID')));
	}
}
